const compose = require('koa-compose');
const Koa = require('koa');
const app = new Koa();

//定义中间件
const loggerMiddleware = async (ctx, next) => {
    const start = Date.now();
    await next();
    const ms = Date.now() - start;
    console.log(ctx.method, ctx.host + ctx.url + ctx.querystring + `==>[${ms}ms]`)
};

const helloMiddleware = async (ctx, next) => {
    // await next();
    ctx.response.type = "text/html"
    ctx.response.body = '<h1>Hello World<h1>';
};


//组合多个中间件，中间件执行区分顺序
const all = compose([loggerMiddleware, helloMiddleware]);
app.use(all);

// app.use([loggerMiddleware, helloMiddleware]);

app.listen(3000, () => {
    console.log('server is running at http://localhost:3000')
});